package us.mooresr.geartrack.client.core.presenter;

import java.util.List;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;

import us.mooresr.geartrack.client.events.ItemCheckedInEvent;
import us.mooresr.geartrack.client.place.NameTokens;
import us.mooresr.geartrack.shared.action.CheckInItemAction;
import us.mooresr.geartrack.shared.action.CheckInItemResult;
import us.mooresr.geartrack.shared.action.GetAllCheckedOutItemsAction;
import us.mooresr.geartrack.shared.action.GetAllCheckedOutItemsResult;
import us.mooresr.geartrack.shared.model.Item;
import us.mooresr.geartrack.shared.model.Person;

import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class ReturnItemPresenter extends
		Presenter<ReturnItemPresenter.MyView, ReturnItemPresenter.MyProxy> 
	implements ReturnUiHandlers {

	public interface MyView extends View, HasUiHandlers<ReturnUiHandlers> {
		public void setItems(List<Item> list);
		public void removeItem(Item item);
	}
	
	private final DispatchAsync dispatcher;

	@ProxyCodeSplit
	@NameToken(NameTokens.returnToken)
	public interface MyProxy extends ProxyPlace<ReturnItemPresenter> {
	}

	@Inject
	public ReturnItemPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		getView().setUiHandlers(this);
		
		this.dispatcher = dispatcher;
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MainPresenter.TYPE_SetMainContent, this);
	}
	
	@Override
	public void onBind() {
		super.onBind();
		
		addRegisteredHandler(ItemCheckedInEvent.getType(), new ItemCheckedInEvent.ItemCheckedInHandler() {
			
			@Override
			public void onItemCheckedIn(ItemCheckedInEvent event) {
				getView().removeItem(event.getItem());
				
			}
		});
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		
		GetAllCheckedOutItemsAction action = new GetAllCheckedOutItemsAction();
		
		dispatcher.execute(action,  new AsyncCallback<GetAllCheckedOutItemsResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetAllCheckedOutItemsResult result) {
				getView().setItems(result.getItems());
				
			}
			
		});
	}

	@Override
	public void returnItem(Long itemId) {
		CheckInItemAction action = new CheckInItemAction(itemId);
		
		dispatcher.execute(action,  new AsyncCallback<CheckInItemResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(CheckInItemResult result) {
				ItemCheckedInEvent.fire(getEventBus(), result.getItemOut());
				
			}
			
		});
		
	}
}
